/**
 * 
 */
package gui;



import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;

import agentInterface.AGVDetails;
import agentInterface.MachineDetails;
import agents.FactoryAgent;


/**
 * @author abeldantas
 * 
 */
public class FactoryGui extends JFrame implements ActionListener {
	private static final long serialVersionUID = 1L;
	FactoryAgent agent;
	Plot plot;

	public FactoryGui(FactoryAgent a) {
		super(a.getLocalName());
		agent = a;

		plot = new Plot(agent);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		getContentPane().setPreferredSize(new Dimension(810, 700));
		setLocation(300, 20);

		JPanel f = new JPanel();
		f.add(plot);

		getContentPane().add(f);

		// p.add(jButton1);
		// getContentPane().add(p, BorderLayout.SOUTH);
		getContentPane().add(initComponents());
		getContentPane().setLayout(
				new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));

	}

	private javax.swing.JPanel jPanel1;
	private javax.swing.JPanel jPanel2;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JScrollPane jScrollPane2;
	private javax.swing.JTable jTable1;
	private javax.swing.JTable jTable2;

	private JPanel initComponents() {

		JPanel masterPanel = new JPanel();
		// masterPanel.setPreferredSize(new Dimension(800,300));
		jPanel1 = new javax.swing.JPanel();
		jScrollPane2 = new javax.swing.JScrollPane();
		jTable2 = new javax.swing.JTable();
		jPanel2 = new javax.swing.JPanel();
		jScrollPane1 = new javax.swing.JScrollPane();
		jTable1 = new javax.swing.JTable();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null,
				"Machines",
				javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
				javax.swing.border.TitledBorder.DEFAULT_POSITION, null,
				new java.awt.Color(255, 0, 0)));


		initializeMachineTableValues();
		
		jScrollPane2.setViewportView(jTable2);

		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
				jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 312,
				Short.MAX_VALUE));
		jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0,
				Short.MAX_VALUE));

		jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null,
				"AVGs", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
				javax.swing.border.TitledBorder.DEFAULT_POSITION, null,
				new java.awt.Color(0, 0, 255)));

		initializeAgvTableValues();

		jScrollPane1.setViewportView(jTable1);

		javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(
				jPanel2);
		jPanel2.setLayout(jPanel2Layout);
		jPanel2Layout.setHorizontalGroup(jPanel2Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 450,
				Short.MAX_VALUE));
		jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 180,
				Short.MAX_VALUE));

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				masterPanel);
		masterPanel.setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						javax.swing.GroupLayout.Alignment.TRAILING,
						layout.createSequentialGroup()
								.addComponent(jPanel2,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(jPanel1,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE,
						javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
				.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
						javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));

		pack();
		return masterPanel;
	}
	public void updateMachineTableValues() {
		DefaultTableModel tableModel = (DefaultTableModel) jTable2.getModel();

		Map<String, MachineDetails> macs = agent.getMachinesDetails();

		int i = 0;
		for (String key : macs.keySet()) {
			MachineDetails mac = macs.get(key);
			tableModel.setValueAt(mac.getName(), i, 0);
			tableModel.setValueAt(mac.getType(), i, 1);
			tableModel.setValueAt("(" + String.format("%.3g%n", mac.getPosition().getX())
					+ ";" + String.format("%.3g%n", mac.getPosition().getY())
					+ ")", i, 2);
			tableModel.setValueAt(mac.getProduct(), i, 3);
			i++;
		}

		tableModel.fireTableDataChanged();
	}
	private void initializeMachineTableValues() {
		Map<String, MachineDetails> macs = agent.getMachinesDetails();

		Object[][] arr = new Object[macs.size()][4];

		int i = 0;
		for (String key : macs.keySet()) {
			MachineDetails mac = macs.get(key);
			arr[i][0] = mac.getName();
			arr[i][1] = mac.getType();
			arr[i][2] = "(" + String.format("%.3g%n", mac.getPosition().getX())
					+ ";" + String.format("%.3g%n", mac.getPosition().getY())
					+ ")";
			arr[i][3] = mac.getProduct();
			i++;
		}

		jTable2.setModel(new javax.swing.table.DefaultTableModel(
				arr,
				new String[] { "Name", "Type", "Position", "Product"}));
		
	}

	public void updateAgvTableValues() {
		DefaultTableModel tableModel = (DefaultTableModel) jTable1.getModel();

		Map<String, AGVDetails> agvs = agent.getAgvsDetails();

		int i = 0;
		for (String key : agvs.keySet()) {
			AGVDetails agv = agvs.get(key);
			tableModel.setValueAt(agv.getName(), i, 0);
			tableModel.setValueAt("(" + String.format("%.3g%n", agv.getPosition().getX())
					+ ";" + String.format("%.3g%n", agv.getPosition().getY())
					+ ")", i, 1);
			tableModel.setValueAt(agv.getProduct(), i, 2);
			tableModel.setValueAt(agv.getCurrentLoad() + "/" + agv.getMaximumLoad(), i, 3);
			tableModel.setValueAt(agv.getCurrentBattery() + "/" + agv.getMaximumBattery(), i, 4);
			i++;
		}

		tableModel.fireTableDataChanged();
	}

	public void initializeAgvTableValues() {

		Map<String, AGVDetails> agvs = agent.getAgvsDetails();

		Object[][] arr = new Object[agvs.size()][5];

		int i = 0;
		for (String key : agvs.keySet()) {
			AGVDetails agv = agvs.get(key);
			arr[i][0] = agv.getName();
			arr[i][1] = "(" + String.format("%.3g%n", agv.getPosition().getX())
					+ ";" + String.format("%.3g%n", agv.getPosition().getY())
					+ ")";
			arr[i][2] = agv.getProduct();
			arr[i][3] = agv.getCurrentLoad() + "/" + agv.getMaximumLoad();
			arr[i][4] = agv.getCurrentBattery() + "/" + agv.getMaximumBattery();
			i++;
		}

		jTable1.setModel(new javax.swing.table.DefaultTableModel(
				arr,
				new String[] { "Name", "Position", "Product", "Load", "Battery" }));
	}

	public void showGui() {
		super.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if ("debug".equals(e.getActionCommand())) {
		}
	}
}
